import traceback

from task.OperateDBHouse import OperateDBHouse
from task.MonitorLikeHouse import MonitorLikeHouse
from optparse import OptionParser
import sys
import os
from baseclass.send_email import SendMail
from base_start import BaseStart


class HouseMonitor(BaseStart):

    def run(self):
        parser = OptionParser()
        parser.add_option("-t", "--type", dest="type",
                          help="xiaoqu or house")
        parser.add_option("-d", "--district", dest="district",
                          help="district")
        parser.add_option("-i", "--min_money", dest="min_money",
                          help="min_money")
        parser.add_option("-a", "--max_money", dest="max_money",
                          help="max_money")
        parser.add_option("-x", "--max_size", dest="max_size",
                          help="max_size")
        parser.add_option("-s", "--min_size", dest="min_size",
                          help="min_size")
        parser.add_option("-q" ,"--xiaoqu_list", dest="xiaoqu_list",
                          help="xiaoqu_list")
        parser.add_option("-l", "--must_look", dest="must_look",
                          help="must_look")
        (options, args) = parser.parse_args()
        type = options.type
        like = MonitorLikeHouse()
        try:
            if type == "hl":
                like.monitor_like()
            elif type == "hs":
                district = options.district
                min_m,max_m = options.min_money,options.max_money
                min_s,max_s = options.min_size,options.max_size
                if min_m is None:
                    min_m = 400
                if max_m is None:
                    max_m = 500
                if min_s is None:
                    min_s = 50
                if max_s is None:
                    max_s = 75
                if options.xiaoqu_list is not None:
                     xiaoqu_id_list = options.xiaoqu_list.split(",")
                else:
                    xiaoqu_id_list =  []
                must_look = options.must_look
                like.monitor_suitable_house(district,min_price=min_m,max_price=max_m,size=min_s,max_size=max_s,xiaoqu_id_list=xiaoqu_id_list,must_look=must_look)
            else:
                like.monitor_xiaoqu_like()
        except Exception as e:
            mail = SendMail()
            print(e)
            mail.send_email("房源监控异常提醒：{0}".format(traceback.print_exc()))


if __name__ == "__main__":
   h = HouseMonitor()
   h.run()

